package battlestein;
import java.awt.Graphics;
import java.util.*;

import missile.Missile;

import base.*;
/**
 * 
 * @author LocoRocoBattleStein
 *
 */

public class AgentManager {
	private LinkedList<Agent> agent_manager;
	private BaseManager bases;
	private Graph graph;
	
	private int ids;
	/**
	 * Constructor
	 * @param bases
	 * @param g
	 */
	public AgentManager(BaseManager bases, Graph g) {
		this.agent_manager = new LinkedList<Agent>();
		this.bases = bases;
		this.graph = g;
		ids = 0;
	}
	
	public void add(Agent a) {
		a.setIdAgent(ids++);
		a.initPath(graph, bases);
		agent_manager.add(a);
		
	}
	
	public void update() { 
		synchronized (agent_manager) {
			LinkedList<Agent> removed = new LinkedList<Agent>();
			
			for(Agent a : agent_manager) {
				if(a.update()) removed.add(a);
				else if(a.getLife() <= 0) removed.add(a);
			}
			
			for(Agent a : removed) {
				agent_manager.remove(a);
			}
		}
	}

	public void draw(Graphics g) {
		synchronized (agent_manager) {
			for(Agent a : agent_manager) {
				a.draw(g);
			}
		}
	}
	
	public LinkedList<Agent> getAgentManager() {
		return agent_manager;
	}
}
